package com.self.campussecondhandmarket.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.self.campussecondhandmarket.entity.domain.Admin;
import com.self.campussecondhandmarket.entity.request.admin.LoginRequest;
import com.self.campussecondhandmarket.resp.Result;
import com.self.campussecondhandmarket.service.AdminService;
import com.self.campussecondhandmarket.mapper.AdminMapper;
import com.self.campussecondhandmarket.utils.JWTUtil;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
* @author 31465
* @description 针对表【tb_admin(管理员表)】的数据库操作Service实现
* @createDate 2024-04-17 15:02:08
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{
	
	@Override
	public Result<String> login(LoginRequest loginRequest) {
		Admin user = this.baseMapper.selectOne(Wrappers.<Admin>lambdaQuery()
				.eq(Admin::getUsername, loginRequest.getUsername())
				.eq(Admin::getPassword, loginRequest.getPassword())
		);
		// 用户名或密码错误
		if (user == null) {
			return Result.error("用户名或密码错误");
		}
		// 登录成功
		Map<String, Object> claims = Map.of("id", user.getId());
		String s = JWTUtil.generateToken(claims);
		return Result.success(s);
	}
}




